package com.ittx.dzt.jinglyun.service;

import cn.hutool.core.util.StrUtil;
import com.ittx.dzt.edi.constant.PlatformMessageConstant;
import com.ittx.dzt.edi.dto.PlatformApiParamDTO;
import com.ittx.dzt.edi.dto.ShipmentExtraPackageDto;
import com.ittx.dzt.edi.vo.ResponseMessageVO;
import com.ittx.dzt.jinglyun.handler.AbstractBaseJinglYunApiHandler;
import com.ittx.dzt.log.monitor.ExceptionLog;
import com.ittx.dzt.sys.response.Result;
import lombok.extern.slf4j.Slf4j;
import open.constant.PlatformApiConstant;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 包裹上传
 * <p>
 * cy.shipment.dispatch.extra.package
 * /open/api/parcel/add 包裹上传
 * 应用场景：通过ERP调用此接口通知鲸灵SCRM新增额外包裹信息
 * 注意：必须发货后可以调用添加包裹接口、必须发货后20天内才可以调用添加包裹接口、最多同一个订单最多上传50个包裹
 * https://jlopen.jinglyun.com/main/doc?docType=2&cateId=10001&docId=20037
 * </p>
 *
 * @author wl_code@163.com
 * @version 1.0
 * @date 2025/10/23
 */
@Slf4j
@Service
public class JinglYunShipmentExtraPackageSerivce extends AbstractBaseJinglYunApiHandler<ShipmentExtraPackageDto> {
	@Override
	public String getJinglMethod() {
		return "/open/api/parcel/add";
	}

	@Override
	public String getLocalApiMethod() {
		return PlatformApiConstant.InboundInterface.SHIPMENT_DISPATCH_EXTRA_PACKAGE;
	}


	@Override
	public Result<?> doProcess(PlatformApiParamDTO platformApiParamDTO, ShipmentExtraPackageDto shipmentExtraPackageDto) {
		try {
			Map<String, Object> params = new HashMap<>();
			List<Map<String, Object>> logisticsDtoListMap = new ArrayList<>();
			Map<String, Object> logisticsDtoMap = new HashMap<>();
			// orderId	Long	是	发货单ID
			logisticsDtoMap.put("orderId", shipmentExtraPackageDto.getSourceOrderCode());
			// logisticsCompanyName	String	是	物流公司名称（非编码）
			logisticsDtoMap.put("logisticsCompanyName", shipmentExtraPackageDto.getCarrierName());
			// logisticsCode	String	是	物流单号
			logisticsDtoMap.put("logisticsCode", shipmentExtraPackageDto.getWaybillCode());
			logisticsDtoListMap.add(logisticsDtoMap);
			params.put("logisticsDTOList", logisticsDtoListMap);
			return this.executeApi(platformApiParamDTO, params, platformApiParamDTO.getRequestId());
		} catch (Exception e) {
			String msg = StrUtil.format(PlatformMessageConstant.MSG_PLAT_0003_MSG, platformApiParamDTO.getRequestId(), this.getPlatformCode(), this.getJinglMethod(), e.getMessage());
			ExceptionLog.error(e, PlatformMessageConstant.MSG_PLAT_0003_MSG, platformApiParamDTO.getRequestId(), this.getPlatformCode(), this.getJinglMethod(), e.getMessage());
			return Result.error(new ResponseMessageVO(PlatformMessageConstant.MSG_PLAT_0003, msg)).addMsg(msg);
		}
	}
}
